package com.yyfax.ph.personal.authority.phonechange;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.common.form.ClearEditText;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.BuildConfig;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.common.utils.Constants.TimerTask;
import com.yyfax.ph.personal.authority.verifycode.VerifyCodeModel;
import com.yyfax.ph.personal.authority.verifycode.VerifyCodePresenter;

public class ChangeOldPhoneActivity extends BaseYYFaxActivity implements VerifyCodePresenter.View {
	private final static String TAG = ChangeOldPhoneActivity.class.getSimpleName();

	private static final int LOAD_ID_VERIFY_CODE = 1;

	/**
	 * 原手机号码
	 */
	@BindView(R.id.old_phone_number_new_tv)
	TextView mOldPhoneNumberNewTv;
	/**
	 * 验证码输入框
	 */
	@BindView(R.id.verify_code_input_clearedt)
	ClearEditText mVerifyCodeInputClearedt;
	/**
	 * 获取验证码按钮
	 */
	@BindView(R.id.get_verify_code_tv)
	TextView mGetVerifyCodeTv;
	/**
	 * 下一步按钮
	 */
	@BindView(R.id.next_step)
	Button mNextStep;

	private VerifyCodePresenter mVerifyCodePresenter;


	public static void start(Activity aty) {
		Intent intent = new Intent(aty, ChangeOldPhoneActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.change_old_phone_activity_new_layout;
	}

	private TimerTask timerTask = new TimerTask() {
		@Override
		public void onTime(int time) {
			mGetVerifyCodeTv.setTextColor(getResources().getColor(R.color.color_999999));
			mGetVerifyCodeTv.setText(getString(R.string.count_decrease, Constants.TIME - time));
			mGetVerifyCodeTv.setClickable(false);
			mGetVerifyCodeTv.setEnabled(false);
		}

		@Override
		public void onEnd() {
			mGetVerifyCodeTv.setText(getString(R.string.re_get_obtain));
			mGetVerifyCodeTv.setClickable(true);
			mGetVerifyCodeTv.setTextColor(getResources().getColor(R.color.color_2b92ec));
			mGetVerifyCodeTv.setEnabled(true);
		}

		public String getKey() {
			return TAG;
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		Constants.timerTask = timerTask;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Constants.timerTask = null;
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new VerifyCodePresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mVerifyCodePresenter = (VerifyCodePresenter) data;
		mVerifyCodePresenter.attach(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOAD_ID_VERIFY_CODE);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOAD_ID_VERIFY_CODE, null, this);
	}

	@Override
	public void initContentView() {
		String userName = YJPH.getInstance().getPreferenceManager().getUserPhone();

		setBtnRightVisible(false);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.change_phone_strs);

		mTitleCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, DensityUtil.dip2px(this, 18));
		mTitleCenter.setTextColor(getResources().getColor(R.color.color_262932));
		mOldPhoneNumberNewTv.setText(userName);
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public VerifyCodeModel.VerifyCodeParams getVerifyParams(String verifyCodeType) {
		VerifyCodeModel.VerifyCodeParams verifyCodeParams = new VerifyCodeModel.VerifyCodeParams();
		verifyCodeParams.phone = mOldPhoneNumberNewTv.getText().toString();
		verifyCodeParams.type = verifyCodeType;
		verifyCodeParams.code = mVerifyCodeInputClearedt.getText().toString();

		return verifyCodeParams;
	}

	@Override
	public void obtainVerifyCodeSuccess(String verifyCode) {
		//debug模式下自动填充 生产模式下不会进行
		if (BuildConfig.DEBUG) {
			mVerifyCodeInputClearedt.setText(verifyCode);
		}

		dismissLoading();
		Constants.startTimer(TAG);
		if (Constants.timerTask == null)
			Constants.timerTask = timerTask;
		showToast(R.string.verification_code_sent);
	}

	@Override
	public void obtainVerifyCodeFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@Override
	public void checkVerifyCodeSuccess(String code) {
		dismissLoading();
		String oldPhone = mOldPhoneNumberNewTv.getText().toString();
		Intent intent = new Intent(this, ChangeNewPhoneActivity.class);
		intent.putExtra(ChangeNewPhoneActivity.OLD_PHONE, oldPhone);
		intent.putExtra(ChangeNewPhoneActivity.VERIFY_ID, code);
		ChangeNewPhoneActivity.start(this, intent);
		finish();
	}

	@Override
	public void checkVerifyCodeFailure(String tips) {
		dismissLoading();
		showToast(tips);
		if (getString(R.string.verify_code_error).equals(tips)) {
			mVerifyCodeInputClearedt.getText().clear();
		}
	}

	@OnClick({R.id.get_verify_code_tv, R.id.next_step})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.get_verify_code_tv:
				mVerifyCodePresenter.obtainVerifyCode(VerifyCodeModel.VERIFY_CODE_TYPE_MODIFY_PHONE_NUM);
				break;
			case R.id.next_step:
				if (!checkInput()) {
					return;
				}
				mVerifyCodePresenter.checkVerifyCode(VerifyCodeModel.VERIFY_CODE_TYPE_MODIFY_PHONE_NUM);
				break;
		}
	}

	private boolean checkInput() {
		String verifyCode = mVerifyCodeInputClearedt.getText().toString().trim();
		if (TextUtils.isEmpty(verifyCode)) {
			showToast(getResources().getString(R.string.please_input_toast, getString(R.string.verify_code)));
			return false;
		} else {
			return true;
		}
	}
}
